package com.example.demo.mapper;

import com.example.demo.entity.Music;
import com.example.demo.entity.MusicQuery;
import com.example.demo.entity.TypeCount;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.session.RowBounds;


import java.util.List;
import java.util.Map;

@Mapper
public interface MusicMapper {



    // 分页条件查询音乐
    @Select("<script>" +
            "SELECT * FROM music_tb " +
            "<where>" +
            "<if test='musicQuery.music_name != null and musicQuery.music_name.trim() != \"\"'>" +
            "AND music_name LIKE CONCAT('%', #{musicQuery.music_name}, '%')" +
            "</if>" +
            "<if test='musicQuery.artist != null and musicQuery.artist.trim() != \"\"'>" +
            "AND artist LIKE CONCAT('%', #{musicQuery.artist}, '%')"+
            "</if>" +
            "<if test='musicQuery.type != null and musicQuery.type.trim() != \"\"'>" +
            "AND type LIKE CONCAT('%', #{musicQuery.type}, '%')" +
            "</if>" +
            "</where>" +
            "</script>")
    List<Music> selectByCondition(@Param("musicQuery") MusicQuery musicQuery);

    @Select("<script>" +
            "SELECT COUNT(*) FROM music_tb " +
            "<where>" +
            "<if test='musicQuery.music_name != null and musicQuery.music_name.trim() != \"\"'>" +
            "AND music_name LIKE CONCAT('%', #{musicQuery.music_name}, '%')" +
            "</if>" +
            "<if test='musicQuery.artist != null and musicQuery.artist.trim() != \"\"'>" +
            "AND artist LIKE CONCAT('%', #{musicQuery.artist}, '%')" +
            "</if>" +
            "<if test='musicQuery.type != null and musicQuery.type.trim() != \"\"'>" +
            "AND type LIKE CONCAT('%', #{musicQuery.type}, '%')" +
            "</if>" +
            "</where>" +
            "</script>")
    int countByCondition(@Param("musicQuery") MusicQuery musicQuery);








    //首页热门歌曲展示
    @Select("SELECT * FROM music_tb ORDER BY duration DESC")
    List<Music> findMusicByDuration();

    //查找所有歌曲信息
    @Select("select * from music_tb")
    List<Music> findMusicAll();

    //根据ID查找歌曲信息
    @Select("select * from music_tb where music_id = #{music_id}")
    Music findMusicID(Integer music_id);



    // 根据多个 ID 查找歌曲信息
    @Select("<script>" +
            "SELECT * FROM music_tb " +
            "WHERE music_id IN " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Music> findMusicsByIds(@Param("ids") List<Integer> ids);



    //方法用来播放次数加一
    @Update("UPDATE music_tb SET duration = duration + 1 WHERE music_id = #{musicId}")
    int incrementPlayCount(@Param("musicId") String musicId);

    // 按热度降序获取所有歌曲
    @Select("SELECT * FROM music_tb ORDER BY popularity DESC")
    List<Music> findMusicByPopularity();

    //添加歌曲信息
    @Insert("insert into music_tb (music_id,music_name,artist,type,duration,lyrics,audio,create_date,description,celebrity_review,cover_image,popularity)" +
            "values (#{music_id},#{music_name},#{artist},#{type},#{duration},#{lyrics},#{audio},sysdate(),#{description},#{celebrity_review},#{cover_image},#{popularity})" )
    Integer insertMusic(Music music);

    //修改歌曲信息
    @Update("update music_tb  set  music_id=#{ music_id},music_name=#{music_name},artist=#{artist},type=#{type},duration=#{duration},lyrics=#{lyrics},audio=#{audio},description=#{description},celebrity_review=#{celebrity_review},cover_image=#{cover_image} where music_id = #{music_id}")
    Integer updateMusic(Music music);

    //删除歌曲信息
    @Delete("delete from music_tb  where music_id = #{music_id}")
    Integer deleteMusic(Integer  music_id);

    @Select("SELECT type, COUNT(*) AS count FROM music_tb GROUP BY type")
    List<TypeCount> findMusicTypeCounts();

    // 根据用户ID统计收藏歌曲种类数量
    @Select("SELECT m.type, COUNT(*) as count FROM music_collection_tb music " +
            "JOIN musics m ON mc.music_id = m.music_id " +
            "WHERE mc.user_id = #{userId} " +
            "GROUP BY m.type")
    List<Map<String, Object>> countMusicTypeByUserId(@Param("userId") Integer userId);

}